﻿/*----------------------------------------------------------------
// Copyright (C) ET
// 版权所有。 
//
// 文件名：StringHelper

// 文件功能描述：字符串操作类

// 创建标识：胡进财 20140822
 
// 修改标识：

// 修改描述：

//----------------------------------------------------------------*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ET.Monitor.Helper
{
    public class StringHelper
    {
        /// <summary>
        /// 计算方法
        /// </summary>
        /// <param name="formule">数字公式</param>
        /// <returns>Value(Type:string)</returns>
        static public string Cale(string formule)
        {
            try
            {
                Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
                return Microsoft.JScript.Eval.JScriptEvaluate(formule, ve).ToString();
            }
            catch (Exception ce)
            {
                return "0";
            }
        }

        /// <summary>
        /// 处理非数字和正无穷大
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static public string checkCatch(string str)
        {
            string s;
            if (str == "非数字")
            { s = "0"; }
            else if (str == "正无穷大")
            { s = "0"; }
            else if (str == "负无穷大")
            { s = "0"; }
            else
            {
                s = str;
            }
            return s;
        }

        /// <summary>
        /// 将数字转换成中文
        /// </summary>
        /// <param name="str">阿拉伯数字</param>
        /// <param name="appendStr">追加字符串</param>
        /// <param name="fontType">type:大写 or any string</param>
        /// <returns></returns>
        public string ConvertChinese(string str, string appendStr, string fontType)
        {
            //"零壹贰叁肆伍陆柒捌玖拾佰仟萬億圆整角分"
            string cstr = "";

            if (fontType == "大写")
            {
                switch (str)
                {
                    case "0": cstr = "零"; break;
                    case "1": cstr = "壹"; break;
                    case "2": cstr = "贰"; break;
                    case "3": cstr = "叁"; break;
                    case "4": cstr = "肆"; break;
                    case "5": cstr = "伍"; break;
                    case "6": cstr = "陆"; break;
                    case "7": cstr = "柒"; break;
                    case "8": cstr = "捌"; break;
                    case "9": cstr = "玖"; break;
                }
            }
            else
            {
                switch (str)
                {
                    case "0": cstr = "零"; break;
                    case "1": cstr = "一"; break;
                    case "2": cstr = "二"; break;
                    case "3": cstr = "三"; break;
                    case "4": cstr = "四"; break;
                    case "5": cstr = "五"; break;
                    case "6": cstr = "六"; break;
                    case "7": cstr = "七"; break;
                    case "8": cstr = "八"; break;
                    case "9": cstr = "九"; break;
                }
            }

            return cstr + appendStr;
        }

        /// <summary>
        /// 显示小数点位数
        /// </summary>
        /// <param name="res"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public string ShowPoint(string res, string num)
        {
            double val = 0;

            if (num != "-1")
            {
                if (num == "")
                    num = "6";

                if (res == "" || res == null)
                { return "0"; }
                else if (res == "0")
                { return "0"; }
                else if (res == "&nbsp;")
                { return "&nbsp;"; }
                else if (res == "0.00")
                { return "0"; }
                else if (res == "非数字")
                { return "0"; }
                else if (res == "正无穷大")
                { return "0"; }
                else if (res == "负无穷大")
                { return "0"; }
                else if (res == "/")
                { return "/"; }
                else
                {
                    try
                    { val = Convert.ToDouble(res); }
                    catch
                    { val = 0; }
                }
                return val.ToString("f" + num);

            }
            else
            {
                if (res == "&nbsp;")
                { return "&nbsp;"; }
                else if (res == "0.00")
                { return "0"; }
                else if (res == "非数字")
                { return "0"; }
                else if (res == "正无穷大")
                { return "0"; }
                else if (res == "负无穷大")
                { return "0"; }

                return res;
            }

        }

        /// 
        /// 将一位数字转换成中文大写数字
        /// 
        public string ConvertChinese(string str)
        {
            //"零壹贰叁肆伍陆柒捌玖拾佰仟萬億圆整角分"
            string cstr = "";

            switch (str)
            {
                //case "0": cstr = "零"; break;
                //case "1": cstr = "壹"; break;
                //case "2": cstr = "贰"; break;
                //case "3": cstr = "叁"; break;
                //case "4": cstr = "肆"; break;
                //case "5": cstr = "伍"; break;
                //case "6": cstr = "陆"; break;
                //case "7": cstr = "柒"; break;
                //case "8": cstr = "捌"; break;
                //case "9": cstr = "玖"; break;

                case "0": cstr = "零"; break;
                case "1": cstr = "一"; break;
                case "2": cstr = "二"; break;
                case "3": cstr = "三"; break;
                case "4": cstr = "四"; break;
                case "5": cstr = "五"; break;
                case "6": cstr = "六"; break;
                case "7": cstr = "七"; break;
                case "8": cstr = "八"; break;
                case "9": cstr = "九"; break;
            }
            return cstr + "值";
        }

        

        /// <summary>
        /// 处理形如'{0}'，而参数中又有单引号的情况，将参数中的单引号double
        /// 如果args中的单引号不是作为文本的话，不要用这个方法，直接用string.format()
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        static public string stringFormat(string format, params object[] args)
        {
            if (format.IndexOf("'") == -1) //没有在单引号内的占位符
                return string.Format(format, args);

            ArrayList al = new ArrayList();
            for (int i = 0; i < args.Length; i++)
                al.Add(args[i].ToString().Replace("'", "''")); // 处理单引号
            return string.Format(format, al.ToArray());
        }

        /// <summary>
        /// 数组排序(冒泡)
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public string[] arraySort(string[] arr)
        {
            string temp;

            for (int j = 0; j < arr.Length; j++)
            {
                for (int i = arr.Length - 1; i > j; i--)
                {
                    if (arr[i] != null && arr[j] != null)
                    {
                        if (double.Parse(arr[j]) < double.Parse(arr[i]))
                        {
                            temp = arr[j];
                            arr[j] = arr[i];
                            arr[i] = temp;
                        }
                    }
                }
            }
            return arr;
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="stringToSub"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public string GetFirstString(string stringToSub, int length)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();
            StringBuilder sb = new StringBuilder();
            int nLength = 0;
            bool isCut = false;
            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    sb.Append(stringChar[i]);
                    nLength += 2;
                }
                else
                {
                    sb.Append(stringChar[i]);
                    nLength = nLength + 1;
                }

                if (nLength > length)
                {
                    isCut = true;
                    break;
                }
            }
            if (isCut)
                return sb.ToString() + ". . .";
            else
                return sb.ToString();
        }
    }
}
